package vn.aptech.leaveManagementSystem.view.admin;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import vn.aptech.leaveManagementSystem.business.dto.Employee;
import vn.aptech.leaveManagementSystem.business.dto.UserLoginInfo;
import vn.aptech.leaveManagementSystem.business.model.MyModel;
import vn.aptech.leaveManagementSystem.dao.DBManager;

import java.awt.HeadlessException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

public class ChangeAdPassword extends JFrame{

	DBManager con =  new DBManager();
	Connection conn;
	String sql;

	JPanel pnlMain;
	private JPasswordField jPasswordOldPass;
	private JPasswordField jPasswordNewPass;
	private JPasswordField jPasswordReNewPass;
	JFrame thisFrame = this;
	private JTextField txtdemopass;
	
	public ChangeAdPassword() {
		// TODO Auto-generated constructor stub
		try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }
		
		pnlMain = new JPanel();
		pnlMain.setLayout(null);



		getContentPane().add(pnlMain);
		
		JLabel lblOldPassword = new JLabel("Old Password :");
		lblOldPassword.setBounds(10, 14, 140, 14);
		pnlMain.add(lblOldPassword);
		
		JLabel lblNewPassword = new JLabel("New Password :");
		lblNewPassword.setBounds(10, 42, 140, 14);
		pnlMain.add(lblNewPassword);
		
		JLabel lblConfirmNewPassword = new JLabel("Confirm New Password :");
		lblConfirmNewPassword.setBounds(10, 70, 139, 14);
		pnlMain.add(lblConfirmNewPassword);
		
		jPasswordOldPass = new JPasswordField();
		jPasswordOldPass.setBounds(137, 11, 227, 20);
		pnlMain.add(jPasswordOldPass);
		jPasswordOldPass.setColumns(10);
		
		jPasswordNewPass = new JPasswordField();
		jPasswordNewPass.setColumns(10);
		jPasswordNewPass.setBounds(137, 39, 227, 20);
		pnlMain.add(jPasswordNewPass);
		
		jPasswordReNewPass = new JPasswordField();
		jPasswordReNewPass.setColumns(10);
		jPasswordReNewPass.setBounds(137, 67, 227, 20);
		pnlMain.add(jPasswordReNewPass);
		
		
		
		
					
					
		

		
		
		
		JButton btnChange = new JButton("CHANGE");
		btnChange.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				/*String oldPass = new String(jPasswordOldPass.getPassword()).trim();
		        String newPassword = new String(jPasswordNewPass.getPassword());
		        String confirmPass = new String(jPasswordReNewPass.getPassword());*/ 
				Employee emp = new Employee();
		        //emp.setPassword(password);
		        char [] oldPass = jPasswordOldPass.getPassword();
		        String oldPassWord = new String(oldPass);
		        char [] newPass = jPasswordNewPass.getPassword();
		        String newPassword = new String(newPass);
		        char [] reNewPass = jPasswordReNewPass.getPassword();
		        String reNewPassword = new String(reNewPass);

		        String password = UserLoginInfo.getUserInfo().getPassword();
		        int idEmp = UserLoginInfo.getUserInfo().getIDEmp();
		        
		       
		        if (oldPassWord.equals(""))
		        {
		            JOptionPane.showMessageDialog(null,"Wrong Old PassWord!","Message",JOptionPane.INFORMATION_MESSAGE);
		           
		        }
		        else	if (newPassword.equals(""))
		        {
		            JOptionPane.showMessageDialog(null,"Wrong New PassWord!","Message",JOptionPane.INFORMATION_MESSAGE);
		            
		        }
		        else	if (reNewPassword.equals(""))
		        {
		            JOptionPane.showMessageDialog(null,"Wrong Re-New PassWord!","Message",JOptionPane.INFORMATION_MESSAGE);
		            
		        }
		        else	if (!newPassword.equals(reNewPassword))
		        {
		            JOptionPane.showMessageDialog(null,"New PassWord and Re-New Password must be the same!","Message",JOptionPane.INFORMATION_MESSAGE);
		            
		        }
		        else	if (!oldPassWord.equals(password))
		        {
		            JOptionPane.showMessageDialog(null,"Invalid Old PassWord!","Message",JOptionPane.INFORMATION_MESSAGE);
		            
		        }
		        else	
		        {
		        	MyModel mymodel =new MyModel();
		        	try {
						mymodel.employees.updateEmployeePassword(idEmp, newPassword);
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
		        	emp.setPassword(newPassword);
		        	JOptionPane.showMessageDialog(null,"Change password success","Message",JOptionPane.INFORMATION_MESSAGE);
                    thisFrame.dispose();
		        }
		   
		        
			} 
		});
		btnChange.setBounds(69, 95, 115, 23);
		btnChange.setIcon(new ImageIcon("icons/gif/16x16/Modify.gif"));
		pnlMain.add(btnChange);
		
		JButton btnCancel = new JButton("CANCEL");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				AdminForm adminForm =  new AdminForm();
				adminForm.setVisible(true);
			}
		});
		btnCancel.setBounds(191, 95, 123, 23);
		btnCancel.setIcon(new ImageIcon("icons/gif/16x16/Cancel.gif"));
		pnlMain.add(btnCancel);
		
		/*txtdemopass = new JTextField();
		txtdemopass.setBounds(133, 11, 140, 20);
		pnlMain.add(txtdemopass);
		txtdemopass.setColumns(10);*/
		Employee emp = new Employee();
		String password = emp.getPassword();
//		txtdemopass.setText(password);
//		
//		String str = UserLoginInfo.getUserInfo().getPassword();
//		txtdemopass.setText(str);
		
		setVisible(true);
		setTitle("Change password Admin");
		setSize(390, 165);
		setLocationRelativeTo(null);
	}
}
